//
//  JXExcellentWorkViewController.m
//  JXAddressApp
//
//  Created by huawei.zheng on 2018/10/27.
//  Copyright © 2018 zhhw_home. All rights reserved.
//

#import "JXExcellentWorkViewController.h"
#import <QMUIKit/UIColor+QMUI.h>
#import "JXNewsTableViewCell.h"
#import "Masonry.h"
#import "EgovaNetworking.h"
#import "JXNewModel.h"
#import "UIImageView+AFNetworking.h"
#import "JXBaseWKWebViewController.h"

@interface JXExcellentWorkViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic, strong) UITableView      *tableView;

@property (nonatomic, strong) NSArray           *models;

@end

@implementation JXExcellentWorkViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initRootView];
    [self initdt];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)initRootView
{
    self.view.backgroundColor = [UIColor qmui_colorWithHexString:@"#EAEAEA"];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [_tableView registerClass:[JXNewsTableViewCell class] forCellReuseIdentifier:@"JXNewsTableViewCell"];
}

- (void)initdt
{
    
    NSString *url = @"http://www.wegis.net/demo//home/baseinfo/getsolicitarticle?type=2";
    [EgovaNetworking getWithUrl:url cache:NO completed:^(NSInteger code, id data, NSString *msg) {
        NSLog(@"%@", data);
        NSArray *values = nil;
        if (code == 0 && data[@"data"]) {
            values = data[@"data"];
        }
        else {
            values = [[self class] defaultDt];
        }
        
        NSMutableArray *list = [NSMutableArray new];
        for (NSDictionary *item in data[@"data"]) {
            JXWorkModel *model = [[JXWorkModel alloc] init];
            model.title = item[@"title"];
            model.wordname = item[@"wordname"];
            model.name = item[@"name"];
            model.fileid = [item[@"fileid"] integerValue];
            model.publishtime = item[@"publishtime"];
            
            [list addObject:model];
        }
        
        
        self.models = [list copy];
        [self.tableView reloadData];
    }];
}

+ (NSArray*)defaultDt
{
    return @[@{
                 @"name": @"王萌萌",
                 @"telephone": @"15874932683",
                 @"identifycard": @"",
                 @"email": @"52856382@qq.com",
                 @"address": @"",
                 @"title": @"中山舰旅游区",
                 @"subtitle": @"",
                 @"wordname": @"底图.png",
                 @"fileid": @(5),
                 @"isshow": @(1),
                 @"publishtime": @"2018-06-05",
                 @"isverify": @(1),
                 @"comment": @"通过，可刊登"
                 }, @{
                 @"name": @"刘治云",
                 @"telephone": @"13572549608",
                 @"identifycard": @"",
                 @"email": @"54082639@qq.com",
                 @"address": @"",
                 @"title": @"浅谈江夏古桥梁",
                 @"subtitle": @"",
                 @"wordname": @"浅谈江夏古桥梁.pdf",
                 @"fileid": @(6),
                 @"isshow": @(1),
                 @"publishtime": @"2018-06-05",
                 @"isverify": @(1),
                 @"comment": @"通过"
                 }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JXWorkModel *model = [_models objectAtIndex:indexPath.row];
    
    JXNewsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"JXNewsTableViewCell"];
    cell.titleLabel.text = model.title;
    cell.dateLabel.text = [[NSString alloc] initWithFormat:@"时间:%@", model.publishtime];
    [cell.imgView setImageWithURL:[NSURL URLWithString:@"http://www.wegis.net/demo/style/jxgeocensus/common/css/images/desktop/demo1_2.png"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JXWorkModel *model = [_models objectAtIndex:indexPath.row];
    
    NSString *fileurl = [[NSString alloc] initWithFormat:@"http://www.wegis.net/demo/view/mobile/dist/index.html#/solicitarticle/%ld/detail", model.fileid];
    
    JXBaseWKWebViewController *vc = [JXBaseWKWebViewController new];
    vc.webUrl = [NSURL URLWithString:fileurl];
    vc.webVCTitle = model.title;
    vc.hidesBottomBarWhenPushed = YES;
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

@end
